package cn.xabad.server.thea.test

import cn.xabad.server.thea.ext.md5
import cn.xabad.server.thea.ext.normal
import cn.xabad.server.thea.http.AppLogService
import feign.Feign
import feign.gson.GsonDecoder
import feign.gson.GsonEncoder
import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.xssf.usermodel.XSSFCell
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import java.io.FileInputStream
import java.io.FileOutputStream
import java.text.DecimalFormat
import java.util.*

fun main5(args: Array<String>) {
    val appLogService = Feign.builder().encoder(GsonEncoder()).decoder(GsonDecoder())
            .requestInterceptor {
//                println("http://120.27.244.103:9302" + it.url() + it.queryLine())
            }
            .target(AppLogService::class.java, "http://120.27.244.103:9302")
    val df = DecimalFormat("0")
    val file = "/Users/xueyong/Downloads/camera_t.xlsx"
    val fileInputStream = FileInputStream(file)
    val workbook = XSSFWorkbook(fileInputStream)
    val sheet = workbook.getSheetAt(2)
    println(sheet.sheetName)
    //
    IntRange(0, sheet.physicalNumberOfRows - 1).forEach {
        val row = sheet.getRow(it)
        val idCell = row.getCell(3)
        val timeCell = row.getCell(5)
        val osCell = row.getCell(10)
        idCell.setCellType(CellType.STRING)
        var id = idCell.stringCellValue
        var os = if (osCell.stringCellValue == "Android") "android" else "ios"
        var startTime = 0L
        var endTime = 0L
        var day = ""
//        id = df.format(idCell.stringCellValue)
        println(id)

        val format = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss")
        val time = DateTime.parse(timeCell.stringCellValue, format)
        day = time.toString("yyyyMMdd")
        startTime = time.millis
        endTime = time.millis + (1000 * 60 * 5)

        val log = appLogService.queryLog(
                id, day, startTime = startTime, endTime = endTime,
                page = 0, size = 100,
                dataAccessToken = "bf4472bf9bb2e63e15303730248c43d34d3652e42e58071bcbaf1bbfc6e362db")
        var starting = false
        var openCameraTime = 0L
        var firstFrameTime = 0L
        if (log.data.content.isEmpty()) {
            var cell: XSSFCell? = row.getCell(13)
            if (cell == null) {
                cell = row.createCell(13)
            }
            cell?.setCellValue("没有埋点")
        } else {
            loop@ for (item in log.data.content) {
                when (item.type) {
                    "profile_set" -> {
                    }
                    "track" -> {
                        when (item.key) {
                            "boxfish_debug" -> {
                                val msg = item.properties.debug_message ?: ""

                                if ("教师上线并匹配成功 状态:2" == msg || msg == "老师已经匹配成功。状态：2") {
                                    starting = !starting
                                }
                                if (starting) {
//                                    logger.debug(content)
                                    if(os == "android"){
                                        if(msg.startsWith("打开摄像头-> ")){
                                            val userMd5 = msg.substringAfter("打开摄像头-> ").trim()
                                            if(userMd5 != md5(id)){
                                                openCameraTime = item.time
                                            }
                                        }
                                        if(msg.startsWith("收到第一帧：")){
                                            var userMd5 = msg.substringAfter("收到第一帧：").substringBefore("开始计时").trim()
                                            if(userMd5 != md5(id)){
                                                firstFrameTime = item.time
                                                println("收到学生首帧,${firstFrameTime - openCameraTime}")
                                            }
                                        }
                                        if(msg.startsWith("老师已经匹配成功。状态：2")){
                                            if(openCameraTime != 0L){
                                                var cell: XSSFCell? = row.getCell(3)
                                                if (cell == null) {
                                                    cell = row.createCell(3)
                                                }
                                                cell?.setCellValue("${item.time - openCameraTime}")
                                                break@loop
                                            }
                                        }
                                    }else{
                                        if(msg.startsWith("开摄像头，user：")){
                                            val userMd5 = msg.substringAfter("user：").substringBefore("，")
                                            if(userMd5 != md5(id)){
                                                openCameraTime = item.time
                                            }
                                        }
                                        if(msg.startsWith("收到首帧，user：")){
                                            val userMd5 = msg.substringAfter("user：").substringBefore("，")
                                            if(userMd5 != md5(id)){
                                                firstFrameTime = item.time
                                                println("收到学生首帧,${firstFrameTime - openCameraTime}")
                                            }
                                        }
                                        if(msg.startsWith("教师上线并匹配成功")){
                                            if(openCameraTime != 0L){
                                                var cell: XSSFCell? = row.getCell(3)
                                                if (cell == null) {
                                                    cell = row.createCell(3)
                                                }
                                                cell?.setCellValue("${item.time - openCameraTime}")
                                                break@loop
                                            }
                                        }
                                    }

                                    when {
//                                        msg.startsWith("打开摄像头->") -> {
//                                            println("打开摄像头->")
//                                        }
//                                        msg.startsWith("开摄像头，user：") -> {
//                                            println("开摄像头")
//                                        }
//                                        msg.startsWith("收到第一帧：") -> {
//                                            println("收到第一帧")
//                                        }
//                                        msg.startsWith("有成员开摄像头，type：1；user：") -> {
//                                            println("有成员开摄像头")
//                                        }
//                                        msg.startsWith("有成员开摄像头") -> {
//                                            println("有成员开摄像头")
//                                        }
//                                        msg.startsWith("收到首帧，user") -> {
//                                            println("收到首帧，user")
//                                        }
//                                        msg.startsWith("收到学生首帧") -> {
//                                            println("收到学生首帧")
//                                        }
                                    }

                                }

                            }
                            "finish_module_achieve" -> {
                            }

                            "student_button_click" -> {

                            }
                            "finish_module" -> {
                            }
                            "online_course_heartbeat" -> {

                            }
                            "student_template_learning_video_open_question" -> {
                            }
                            else -> {
                            }
                        }
                    }
                    else -> {

                    }
                }
            }
        }
    }
    fileInputStream.close()
    val fileOut = FileOutputStream(file)
    workbook.write(fileOut)
    fileOut.flush()
    fileOut.close()
}